package com.honghe.system.service.impl;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.honghe.system.domain.vo.ContactVolumeTrendVO;
import com.honghe.system.domain.vo.ContactVolumeTrendQueryVO;
import com.honghe.system.mapper.CustomerContactRecordMapper;
import com.honghe.system.service.IContactVolumeTrendService;

/**
 * 联系总量趋势Service实现
 */
@Service
public class ContactVolumeTrendServiceImpl implements IContactVolumeTrendService {
    
    @Autowired
    private CustomerContactRecordMapper customerContactRecordMapper;

    @Override
    public ContactVolumeTrendVO getContactVolumeTrend(ContactVolumeTrendQueryVO queryVO) {
        // 处理日期范围
        String startDate;
        String endDate;
        
        if ("custom".equals(queryVO.getTimeType())) {
            startDate = queryVO.getStartDate();
            endDate = queryVO.getEndDate();
        } else {
            // 根据时间维度类型计算日期范围
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate endLocalDate = LocalDate.now();
            LocalDate startLocalDate;
            
            switch (queryVO.getTimeType()) {
                case "month":
                    startLocalDate = endLocalDate.minusMonths(12);
                    break;
                case "quarter":
                    startLocalDate = endLocalDate.minusMonths(36);
                    break;
                case "halfYear":
                    startLocalDate = endLocalDate.minusMonths(36);
                    break;
                case "year":
                    startLocalDate = endLocalDate.minusYears(5);
                    break;
                default:
                    startLocalDate = endLocalDate.minusMonths(12);
            }
            
            startDate = startLocalDate.format(formatter);
            endDate = endLocalDate.format(formatter);
        }

        // 查询数据
        List<Map<String, Object>> resultList;
        switch (queryVO.getTimeType()) {
            case "month":
                resultList = customerContactRecordMapper.selectContactVolumeByMonth(startDate, endDate);
                break;
            case "quarter":
                resultList = customerContactRecordMapper.selectContactVolumeByQuarter(startDate, endDate);
                break;
            case "halfYear":
                resultList = customerContactRecordMapper.selectContactVolumeByHalfYear(startDate, endDate);
                break;
            case "year":
                resultList = customerContactRecordMapper.selectContactVolumeByYear(startDate, endDate);
                break;
            default:
                resultList = customerContactRecordMapper.selectContactVolumeByCustom(startDate, endDate);
        }

        // 转换结果
        ContactVolumeTrendVO trendVO = new ContactVolumeTrendVO();
        if (resultList != null && !resultList.isEmpty()) {
            List<String> timeLabels = new ArrayList<>();
            List<Integer> values = new ArrayList<>();
            
            for (Map<String, Object> result : resultList) {
                timeLabels.add(String.valueOf(result.get("time_label")));
                values.add(((Number) result.get("contact_count")).intValue());
            }
            
            trendVO.setTimeLabels(timeLabels);
            trendVO.setValues(values);
        } else {
            trendVO.setTimeLabels(new ArrayList<>());
            trendVO.setValues(new ArrayList<>());
        }
        
        return trendVO;
    }
} 